package gwu.android;


import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;
import java.net.UnknownHostException;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

/**
 * This class connects to a server and receives an article to display.  
 * @author Taylor
 *
 */
public class ReadTextClient extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.read_article);
		String articleText = "test";
		TextView articleTextBox = (TextView) findViewById(R.id.articleText);
//        try {
//			Socket clientSocket = new Socket("10.0.2.2", 6791);
//			DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());
//	        BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
//	        
//	        outToServer.writeBytes("17331");
//	        articleText = inFromServer.readLine();
//	        //TextView articleTextBox = (TextView) findViewById(R.id.articleText);
//			articleTextBox.setText(articleText);
//	        
//		} catch (UnknownHostException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		} catch (IOException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}

		articleTextBox.setText(articleText);
		//Article article = ArticleManager.getInstance().getArticle(17331);
		//ArrayList<Object_WordClozeItem> items = WordClozeGenerator.getInstance().clozeText(article.getArticleText(), article.getLexile());
		
//		TextView articleTextBox = (TextView) findViewById(R.id.articleText);
		Button cButton1 = (Button) findViewById(R.id.choice1);
		Button cButton2 = (Button) findViewById(R.id.choice2);
		Button cButton3 = (Button) findViewById(R.id.choice3);
		Button cButton4 = (Button) findViewById(R.id.choice4);

		//articleTextBox.setText(article.getArticleText());
		//cButton1.setText(items.get(0).getCorrectWord());
		//cButton2.setText(items.get(0).getFoil(0));
		//cButton3.setText(items.get(0).getFoil(1));
		//cButton4.setText(items.get(0).getFoil(2));
		
		
	}
	public void onMyButtonClick(View view)
    {
		new Thread(new Runnable() {
			public void run() {
				String articleText = "test";
				TextView articleTextBox = (TextView) findViewById(R.id.articleText);
		        try {
					Socket clientSocket = new Socket("10.0.2.2", 6791);

					DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());
			        BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
			        
			        outToServer.writeBytes("17331");
			        articleText = inFromServer.readLine();
					articleTextBox.setText(articleText);
			        
				} catch (UnknownHostException e) {
					e.printStackTrace();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}).start();
    }
}

